package com.xai.pg.service;

import com.xai.pg.data.DBInfo;
import com.xai.pg.data.DBTable;
import com.xai.pg.util.Conf;
import com.xai.pg.util.FileUtil;
import com.xai.pg.util.VMUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

@Service
public class JPAGeneratorService {

    @Autowired
    private DBInfoService dbService;
    @Autowired
    private PublicGeneratorService pubService;

    private String javaBaseDir;
    private DBInfo info;

    public void generator() {
        Logger.getLogger(JPAGeneratorService.class.getName()).log(Level.INFO, "==== JPAGeneratorService Generator ===================================================");
        this.javaBaseDir = Conf.getJavaBasePath();
        this.info = dbService.loadDBInfo();
        this.pubService.generator();
        this.generatorAbsClass();
        this.generatorTable();
    }

    public void generatorTable() {
        if (info == null) {
            return;
        }
        List<DBTable> list = info.getTables();
        if (list == null || list.isEmpty()) {
            return;
        }

        list.stream().forEach(table -> {
            generatorTableInfo(table);
        });
    }

    private void generatorAbsClass() {
        String group = Conf.getGroup();
        Map<String, Object> obj = new HashMap<>(4);
        obj.put("group", group);

        String entityStr = VMUtil.toStr(obj, "vm/jpa/AbsEntity.vm");
        FileUtil.createFile(javaBaseDir + "/data/entity/AbsEntity.java");
        FileUtil.writeToFile(javaBaseDir + "/data/entity/AbsEntity.java", entityStr);

        String daoStr = VMUtil.toStr(obj, "vm/jpa/AbsDao.vm");
        FileUtil.createFile(javaBaseDir + "/service/dao/AbsDao.java");
        FileUtil.writeToFile(javaBaseDir + "/service/dao/AbsDao.java", daoStr);

        String serviceStr = VMUtil.toStr(obj, "vm/jpa/AbsService.vm");
        FileUtil.createFile(javaBaseDir + "/service/AbsService.java");
        FileUtil.writeToFile(javaBaseDir + "/service/AbsService.java", serviceStr);
    }

    private void generatorTableInfo(DBTable table) {
        String group = Conf.getGroup();
        String tName = table.getcName();
        Map<String, Object> obj = new HashMap<>(4);
        obj.put("group", group);
        obj.put("table", table);

        String entityStr = VMUtil.toStr(obj, "vm/jpa/entity.vm");
        FileUtil.createFile(javaBaseDir + "/data/entity/" + tName + ".java");
        FileUtil.writeToFile(javaBaseDir + "/data/entity/" + tName + ".java", entityStr);

        String dataStr = VMUtil.toStr(obj, "vm/jpa/data.vm");
        FileUtil.createFile(javaBaseDir + "/data/data/" + tName + "Data.java");
        FileUtil.writeToFile(javaBaseDir + "/data/data/" + tName + "Data.java", dataStr);

        String daoStr = VMUtil.toStr(obj, "vm/jpa/dao.vm");
        FileUtil.createFile(javaBaseDir + "/service/dao/" + tName + "Dao.java");
        FileUtil.writeToFile(javaBaseDir + "/service/dao/" + tName + "Dao.java", daoStr);

        String serviceStr = VMUtil.toStr(obj, "vm/jpa/service.vm");
        FileUtil.createFile(javaBaseDir + "/service/" + tName + "Service.java");
        FileUtil.writeToFile(javaBaseDir + "/service/" + tName + "Service.java", serviceStr);

        String controlStr = VMUtil.toStr(obj, "vm/jpa/controller.vm");
        FileUtil.createFile(javaBaseDir + "/gateway/" + tName + "Controller.java");
        FileUtil.writeToFile(javaBaseDir + "/gateway/" + tName + "Controller.java", controlStr);
    }
}
